package jamirr.loader;

import java.util.function.Supplier;

public interface ServiceDefinition<T> {
    /**
     * @return The full class name of the service
     */
    String getName();

    /**
     * @return is the service present
     */
    default boolean isPresent() {
        return false;
    }

    /**
     * Load the service of throw the given exception.
     *
     * @param exceptionSupplier The exception supplier
     * @param <X>               The exception type
     * @return The instance
     * @throws X The exception concrete type
     */
    default <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
        try {
            return load();
        } catch (Throwable err) {
            throw exceptionSupplier.get();
        }
    }

    /**
     * @return load the service
     */
    T load();
}
